package InterconectionSubsystem;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Sender {
	public static void send(String ip, int port, String message)
	{
		try{
			DatagramSocket sock = new DatagramSocket();
			byte[] msg = message.getBytes();
			DatagramPacket packet = new DatagramPacket(msg, msg.length, InetAddress.getByName(ip), port);
			sock.send(packet);
			System.out.println("Package sent.");
		}catch(Exception e){ System.out.print("Error sending packet: " + e.getMessage() + "\n"); }
	}
	
	public static String toString(InetAddress ip)
	{
		byte[] b_ip = ip.getAddress();
		String s_ip = new String();
		for (int i=0; i < 4 ; i++)
		{
			int n_ip_i = b_ip[i];
			if (n_ip_i < 0) n_ip_i = 256 + n_ip_i;
			s_ip += String.valueOf(n_ip_i) + ".";
		}
		return s_ip.substring(0, s_ip.length()-1);	
	}
}
